﻿package com.tudou.home.newskin
{
    import com.tudou.home.newskin.consts.*;
    import flash.events.*;
    import flash.ui.*;
    import flash.utils.*;
    import gs.*;

    public class SkinBrain extends Object
    {
        private var moveState:String;
        public var percent:Number;
        private var moveTimer:Timer;
        private var homeSkin:HomeNewSkin;
        private var nextItemTimer:Timer;
        private static const MOVING_ON:String = "moving on";
        private static const MOVED_OUT:String = "moved out";
        private static const MOVED_ON:String = "moved on";
        private static const MOVING_OUT:String = "moving out";

        public function SkinBrain(param1:HomeNewSkin)
        {
            homeSkin = param1;
            moveTimer = new Timer(NewSkinConsts.MOUSE_MOVE_TIME);
            return;
        }// end function

        function stopListenMove() : void
        {
            homeSkin.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
            homeSkin.stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseMoveHandler);
            moveTimer.removeEventListener(TimerEvent.TIMER, moveTimerHandler);
            killMove();
            Mouse.show();
            return;
        }// end function

        private function moveOnCompleted() : void
        {
            moveState = MOVED_ON;
            moveTimer.reset();
            moveTimer.start();
            return;
        }// end function

        private function onUpdateHlr() : void
        {
            homeSkin.move(percent);
            return;
        }// end function

        private function moveOutCompleted() : void
        {
            moveState = MOVED_OUT;
            Mouse.hide();
            return;
        }// end function

        private function killMove() : void
        {
            TweenLite.killTweensOf(this);
            return;
        }// end function

        private function moveOn() : void
        {
            moveState = MOVING_ON;
            percent = 0;
            var _loc_1:Object = {percent:1, onUpdate:onUpdateHlr, onComplete:moveOnCompleted};
            TweenLite.to(this, 0.4, _loc_1);
            return;
        }// end function

        public function reset() : void
        {
            return;
        }// end function

        private function moveTimerHandler(event:TimerEvent) : void
        {
            moveTimer.stop();
            if (homeSkin.canMoveOut())
            {
                moveOut();
            }
            return;
        }// end function

        function startListenMove() : void
        {
            homeSkin.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
            homeSkin.stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseMoveHandler);
            moveTimer.addEventListener(TimerEvent.TIMER, moveTimerHandler);
            moveState = MOVED_ON;
            moveTimer.reset();
            moveTimer.start();
            Mouse.show();
            return;
        }// end function

        private function mouseMoveHandler(event:MouseEvent) : void
        {
            Mouse.show();
            switch(moveState)
            {
                case MOVED_ON:
                {
                    moveTimer.reset();
                    moveTimer.start();
                    break;
                }
                case MOVED_OUT:
                {
                    moveOn();
                    break;
                }
                case MOVING_ON:
                {
                    break;
                }
                case MOVING_OUT:
                {
                    killMove();
                    moveOn();
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function moveOut() : void
        {
            moveState = MOVING_OUT;
            percent = 1;
            var _loc_1:Object = {percent:0, onUpdate:onUpdateHlr, onComplete:moveOutCompleted};
            TweenLite.to(this, 0.4, _loc_1);
            return;
        }// end function

    }
}
